﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BKH.BLL
{
    class BenhNhanBUO : IBUO<tbl_BenhNhan>
    {
        HospitalDBDataContext context = new HospitalDBDataContext();
        public bool Insert(tbl_BenhNhan obj)
        {
            try
            {
                context.tbl_BenhNhans.InsertOnSubmit(obj);
                context.SubmitChanges();
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        public bool Delete(int id)
        {
            try
            {
                tbl_BenhNhan obj = context.tbl_BenhNhans.Where(n => n.BenhNhanID == id).First<tbl_BenhNhan>();
                if (obj!=null)
                {
                    context.tbl_BenhNhans.DeleteOnSubmit(obj);
                    context.SubmitChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public bool Delete(tbl_BenhNhan obj)
        {
            throw new NotImplementedException();
        }

        public List<tbl_BenhNhan> GetAll()
        {
            return context.tbl_BenhNhans.ToList<tbl_BenhNhan>();
        }

        public tbl_BenhNhan GetSingle(int id)
        {
            return context.tbl_BenhNhans.Where(n => n.BenhNhanID == id).ToList<tbl_BenhNhan>().FirstOrDefault();
        }

        public bool Update(int id, tbl_BenhNhan obj)
        {
            try
            {
                tbl_BenhNhan bn = context.tbl_BenhNhans.Where(n => n.BenhNhanID == id).ToList<tbl_BenhNhan>().FirstOrDefault();
                bn = obj;
                context.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }

        public List<tbl_BenhNhan> Search(int Id)
        {

            return context.tbl_BenhNhans.Where(n => n.BenhNhanID == Id).ToList<tbl_BenhNhan>();
        }
        public List<tbl_BenhNhan> Search(String Name)
        {

            //return context.tbl_BenhNhans.Select(n => n.LastName.Equals(Name)).ToList<tbl_BenhNhan>();
            throw new NotImplementedException();
            
        }
<<<<<<< .mine

        public List<tbl_BenhNhan> Search(int Id)
        {

            return context.tbl_BenhNhans.Where(n => n.BenhNhanID == Id).ToList<tbl_BenhNhan>();
        }
        public List<tbl_BenhNhan> Search(String Name)
        {

            return context.tbl_BenhNhans.Where(n => n.LastName.Equals(Name)).ToList<tbl_BenhNhan>();
        }

        public List<tbl_BenhNhan> SearchByCMND(String CMND)
        {

            return context.tbl_BenhNhans.Where(n => n.CMND.Equals(CMND)).ToList<tbl_BenhNhan>();
        }

        public String GetCMND(int idbn)
        {
            return context.tbl_BenhNhans.Where(n => n.BenhNhanID == idbn).Select(m => m.CMND).FirstOrDefault();

        }

        
=======
<<<<<<< .mine

        public List<tbl_BenhNhan> SearchByCMND(String CMND)
        {

            return context.tbl_BenhNhans.Where(n => n.CMND.Equals(CMND)).ToList<tbl_BenhNhan>();
        }

        public String GetCMND(int idbn)
        {
            return context.tbl_BenhNhans.Where(n => n.BenhNhanID == idbn).Select(m => m.CMND).FirstOrDefault();

        }

        public tbl_BenhNhan GetMaxIdBN()
        {
            return context.tbl_BenhNhans.OrderByDescending(n => n.BenhNhanID).ToList<tbl_BenhNhan>().FirstOrDefault();
        }
        
=======

        public List<tbl_BenhNhan> Search(int Id)
        {

            return context.tbl_BenhNhans.Where(n => n.BenhNhanID == Id).ToList<tbl_BenhNhan>();
        }
        public List<tbl_BenhNhan> Search(String Name)
        {

            return context.tbl_BenhNhans.Where(n => n.LastName.Equals(Name)).ToList<tbl_BenhNhan>();
        }

        public List<tbl_BenhNhan> SearchByCMND(String CMND)
        {

            return context.tbl_BenhNhans.Where(n => n.CMND.Equals(CMND)).ToList<tbl_BenhNhan>();
        }

        public String GetCMND(int idbn)
        {
            return context.tbl_BenhNhans.Where(n => n.BenhNhanID == idbn).Select(m => m.CMND).FirstOrDefault();

        }

        
>>>>>>> .r20
>>>>>>> .r21
    }
}
